//************************************************************************
//Developed by Guo Ming Li 2009/2010
//Source code:
//  The source code may be freely modified or used but this credit section may not be removed 
//  The original author must be notified of any modifications plus a copy of 
//  the changed source code
//Contact info: mailto://gming.li@gmail.com
//************************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using GML.BizTalk.OperationManager.Library;
using GML.BizTalk.OperationManager.MSBTSOperations;
using System.Collections;


namespace GML.BizTalk.OperationManager.MSBTSOperations
{
    public partial class PerformActions : Form
    {
        public PerformActions()
        {
            InitializeComponent();
            Logger.Configure();
            int index = 0;
            foreach (string item in cb_lastBTSDeployment.Items)
            {
                if (item.ToUpper().Equals("NO"))
                {
                    cb_lastBTSDeployment.SelectedIndex = index;
                    break;
                }
                else
                {
                    index++;
                }
            }
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.ShowDialog();
        }

        private MSBTSOperations oOps = null;
        private Environment[] envInfos = null;

        private void openFileDialog1_FileOK(object sender, CancelEventArgs e)
        {
            this.tbFilename.Text = this.openFileDialog1.FileName.ToString();
            this.btnPerformAction.Enabled = true;
            XmlDocument oDoc = new XmlDocument();
            List<string> ds = new List<string>();
            string sXmlFileName = this.tbFilename.Text.Trim();

            try
            {
                LogHelper(
                        "--------------------------------------------------------------------------------------------" + System.Environment.NewLine
                        + "|" + "Using Instruction XML (" + sXmlFileName + ")" + System.Environment.NewLine +
                        "--------------------------------------------------------------------------------------------" + System.Environment.NewLine,
                        Logger.ServerityLevel.INFO
                        );
                //this.tbProgress.Text += "Using Instruction XML (" + sXmlFileName + ")" + System.Environment.NewLine;
                //Logger.doLog(Guid.NewGuid().ToString(), "Using Instruction XML (" + sXmlFileName + ")", Logger.ServerityLevel.INFO);

                oDoc.Load(sXmlFileName);

                oOps = (MSBTSOperations)GML.Common.Utilities.Xml.Deserialize2Object(oDoc.InnerXml.Trim(), typeof(MSBTSOperations));

                envInfos = oOps.EnvironmentInfo;

                if (envInfos != null)
                {
                    foreach (Environment env in envInfos)
                    {
                        ds.Add(env.EnvID);
                    }
                }
                else
                {
                    ds.Add("Default");
                }

                this.cbEnvInfo.DataSource = ds;

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                oDoc = null;
                sXmlFileName = null;
                ds = null;
            }
        }
        private BtsServer[] GetBtsServerForEnvironment(string sEnvID)
        {
            foreach (Environment env in this.envInfos)
            {
                if (env.EnvID == sEnvID)
                {
                    return env.BtsServers;
                }
            }
            return null;
        }

        private void LogHelper(string message, Logger.ServerityLevel serverity)
        {
            this.tbProgress.AppendText(message);

            Logger.doLog(Guid.NewGuid().ToString(), message, serverity);
        }

        private void PerformAction_ReceiveLocations(Operation op, string sEnvID, string dbServerName, string mgntDbName)
        {
            Exception outException = null;
            ReceiveLocations receiveLocations = null;
            ReceiveLocation receiveLocation = null;
            bool blnOperationSuccessful = false;
            try
            {
                LogHelper(
                "--------------------------------------------------------------------------------------------" + System.Environment.NewLine
                + "|" + System.Environment.NewLine,
                Logger.ServerityLevel.INFO
                );
                receiveLocations = (ReceiveLocations)op.Item;
                foreach (ReceiveLocation _receiveLocation in receiveLocations.ReceiveLocation)
                {
                    if (_receiveLocation.EnvID.Equals(sEnvID))
                    {
                        receiveLocation = _receiveLocation;
                        break;
                    }
                }

                LogHelper(
                    "Found operation [" + op.ExecSequence + "] type [" + receiveLocation.Action.ToString() + " receive location(s)]: ........",
                    Logger.ServerityLevel.INFO
                    );
                try
                {
                    if (receiveLocation.ReceiveLocationProperties == null
                        && receiveLocation.ReceivePortName == null
                        && receiveLocation.Action.Equals(ReceiveLocationActionType.ENABLE_ALL))
                    {
                        blnOperationSuccessful = Library.ReceiveLocations.EnableAllReceiveLocation(
                            dbServerName,
                            mgntDbName,
                            receiveLocation.ApplicatonName,
                            out outException);
                    }
                }
                catch (Exception exception)
                {
                    blnOperationSuccessful = false;
                    outException = exception;
                }

                this.tbProgress.Text += "Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "Operation " + (blnOperationSuccessful ? "successful" : "failed"), Logger.ServerityLevel.INFO);
                if (!blnOperationSuccessful && outException != null)
                {
                    this.tbProgress.Text += outException.Message + System.Environment.NewLine;
                    this.tbProgress.Text += outException.StackTrace + System.Environment.NewLine;
                    Logger.doLog(Guid.NewGuid().ToString(), outException.Message, Logger.ServerityLevel.ERROR);
                    Logger.doLog(Guid.NewGuid().ToString(), outException.StackTrace, Logger.ServerityLevel.ERROR);
                }
                this.tbProgress.Text += "|" + System.Environment.NewLine;
                this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);

            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                outException = null;
                receiveLocations = null;
                receiveLocation = null;
            }
        }

        private void PerformAction_SendPorts(Operation op, string sEnvID, string dbServerName, string mgntDbName)
        {
            Exception outException = null;
            SendPorts sendPorts = null;
            SendPort sendPort = null;
            try
            {
                bool blnOperationSuccessful = false;
                LogHelper(
                        "--------------------------------------------------------------------------------------------" + System.Environment.NewLine
                        + "|" + System.Environment.NewLine,
                        Logger.ServerityLevel.INFO
                        );
                sendPorts = (SendPorts)op.Item;
                foreach (SendPort _sendPort in sendPorts.SendPort)
                {
                    if (_sendPort.EnvID.Equals(sEnvID))
                    {
                        sendPort = _sendPort;
                        break;
                    }
                }

                LogHelper(
                    "Found operation [" + op.ExecSequence + "] type [" + sendPort.Action.ToString() + " send port(s)]: ........",
                    Logger.ServerityLevel.INFO
                    );
                try
                {
                    if (sendPort.SendPortProperties == null && sendPort.Action.Equals(SendPortActionType.START_ALL))
                    {
                        blnOperationSuccessful = Library.SendPorts.StartAllSendPorts(
                            dbServerName,
                            mgntDbName,
                            sendPort.ApplicatonName,
                            out outException);
                    }
                }
                catch (Exception exception)
                {
                    blnOperationSuccessful = false;
                    outException = exception;
                }

                this.tbProgress.Text += "Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "Operation " + (blnOperationSuccessful ? "successful" : "failed"), Logger.ServerityLevel.INFO);
                if (!blnOperationSuccessful && outException != null)
                {
                    this.tbProgress.Text += outException.Message + System.Environment.NewLine;
                    this.tbProgress.Text += outException.StackTrace + System.Environment.NewLine;
                    Logger.doLog(Guid.NewGuid().ToString(), outException.Message, Logger.ServerityLevel.ERROR);
                    Logger.doLog(Guid.NewGuid().ToString(), outException.StackTrace, Logger.ServerityLevel.ERROR);
                }
                this.tbProgress.Text += "|" + System.Environment.NewLine;
                this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);

            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                outException = null;
                sendPorts = null;
                sendPort = null;
            }
        }

        private void PerformAction_HostInstances(Operation op, string sEnvID, string dbServerName, string mgntDbName)
        {
            Exception outException = null;
            HostInstances hostInstances = (HostInstances)op.Item;
            HostInstance hostInstance = null;
            try
            {
                bool blnOperationSuccessful = false;
                LogHelper(
                        "--------------------------------------------------------------------------------------------" + System.Environment.NewLine
                        + "|" + System.Environment.NewLine,
                        Logger.ServerityLevel.INFO
                        );
                hostInstances = (HostInstances)op.Item;
                foreach (HostInstance _hostInstance in hostInstances.HostInstance)
                {
                    if (_hostInstance.EnvID.Equals(sEnvID))
                    {
                        hostInstance = _hostInstance;
                        break;
                    }
                }

                LogHelper(
                    "Found operation [" + op.ExecSequence + "] type [" + hostInstance.Action.ToString() + " host instance(s)]: ........",
                    Logger.ServerityLevel.INFO
                    );
                try
                {
                    if (hostInstance.HostInstanceProperties == null && hostInstance.Action.Equals(HostInstanceActionType.START_ALL))
                    {
                        blnOperationSuccessful = Library.HostInstances.StartAllHostInstances(out outException);
                    }
                }
                catch (Exception exception)
                {
                    blnOperationSuccessful = false;
                    outException = exception;
                }

                this.tbProgress.Text += "Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "Operation " + (blnOperationSuccessful ? "successful" : "failed"), Logger.ServerityLevel.INFO);
                if (!blnOperationSuccessful && outException != null)
                {
                    this.tbProgress.Text += outException.Message + System.Environment.NewLine;
                    this.tbProgress.Text += outException.StackTrace + System.Environment.NewLine;
                    Logger.doLog(Guid.NewGuid().ToString(), outException.Message, Logger.ServerityLevel.ERROR);
                    Logger.doLog(Guid.NewGuid().ToString(), outException.StackTrace, Logger.ServerityLevel.ERROR);
                }
                this.tbProgress.Text += "|" + System.Environment.NewLine;
                this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);

            }
            catch (Exception ex)
            {
                
                throw ex;
            }
            finally
            {
                outException = null;
                hostInstances = null;
                hostInstance = null;
            }
        }

        private void PerformAction_ServiceInstances(Operation op, string sEnvID, string dbServerName, string mgntDbName)
        {
            Exception outException = null;
            ServiceInstances serviceInstances = (ServiceInstances)op.Item;
            ServiceInstance serviceInstance = null;
            bool blnOperationSuccessful = false;
            try
            {
                LogHelper(
                "--------------------------------------------------------------------------------------------" + System.Environment.NewLine
                + "|" + System.Environment.NewLine,
                Logger.ServerityLevel.INFO
                );
                serviceInstances = (ServiceInstances)op.Item;
                foreach (ServiceInstance _serviceInstance in serviceInstances.ServiceInstance)
                {
                    if (_serviceInstance.EnvID.Equals(sEnvID))
                    {
                        serviceInstance = _serviceInstance;
                        break;
                    }
                }

                LogHelper(
                    "Found operation [" + op.ExecSequence + "] type [" + serviceInstance.Action.ToString() + " service instance(s)]: ........",
                    Logger.ServerityLevel.INFO
                    );
                try
                {
                    if (serviceInstance.ServiceTypes == null && serviceInstance.Action.Equals(ServiceInstanceActionType.RESUME_ALL_ADMIN_SUSPENDED))
                    {
                        blnOperationSuccessful = Library.ServiceInstances.StartAllAdminSuspendedInstances(
                                                dbServerName,
                                                mgntDbName,
                                                out outException);
                    }
                }
                catch (Exception exception)
                {
                    blnOperationSuccessful = false;
                    outException = exception;
                }

                this.tbProgress.Text += "Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "Operation " + (blnOperationSuccessful ? "successful" : "failed"), Logger.ServerityLevel.INFO);
                if (!blnOperationSuccessful && outException != null)
                {
                    this.tbProgress.Text += outException.Message + System.Environment.NewLine;
                    this.tbProgress.Text += outException.StackTrace + System.Environment.NewLine;
                    Logger.doLog(Guid.NewGuid().ToString(), outException.Message, Logger.ServerityLevel.ERROR);
                    Logger.doLog(Guid.NewGuid().ToString(), outException.StackTrace, Logger.ServerityLevel.ERROR);
                }
                this.tbProgress.Text += "|" + System.Environment.NewLine;
                this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);

            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                outException = null;
                serviceInstances = null;
                serviceInstance = null;
            }
        }
        private void PerformAction_GACListCompare(Operation op, string sEnvID, string dbServerName, string mgntDbName)
        {
            Exception outException = null;
            Hashtable gacLists = new Hashtable();
            GACList gacList = null;
            GACVerification gacVerification = null;
            GACEntry[] gacEntries = null;
            BtsServer[] btsServers = null;
            List<GACEntry> missingGacEntries = null;
            //Gac serviceInstance = null;
            bool blnOperationSuccessful = false;

            try
            {
                LogHelper(
                "--------------------------------------------------------------------------------------------" + System.Environment.NewLine
                + "|" + System.Environment.NewLine,
                Logger.ServerityLevel.INFO
                );
                LogHelper(
                    "Found operation [" + op.ExecSequence + "] type [" + Library.OperationsType.GACVerification.ToString() + "]: ........",
                    Logger.ServerityLevel.INFO
                    );


                gacVerification = (GACVerification)op.Item;
                gacEntries = gacVerification.Assemblies;
                btsServers = this.GetBtsServerForEnvironment(sEnvID);

                //get the list of assembly from GAC on each computer
                foreach (BtsServer btsServer in btsServers)
                {
                    gacList = new GACList();
                    missingGacEntries = new List<GACEntry>();
                    string machineName = btsServer.ComputerName;
                    string filter = gacVerification.GACListFilter;

                    if (Library.NetTools.IsValidMachineName(machineName))
                    {
                        gacList.LoadList(machineName, filter + "*");
                        missingGacEntries = gacList.GetMissingEntry(gacEntries, gacVerification.VerificationFilter);
                        if (missingGacEntries.Count > 0)
                        {
                            blnOperationSuccessful = false;
                            this.tbProgress.Text += machineName + " is missing the following Assemblies in GAC." + System.Environment.NewLine;
                            Logger.doLog(Guid.NewGuid().ToString(), machineName + " is missing the following Assemblies in GAC.", Logger.ServerityLevel.ERROR);
                            foreach (GACEntry ge in missingGacEntries)
                            {
                                this.tbProgress.Text += ge.ToString(gacVerification.VerificationFilter) + System.Environment.NewLine;
                                Logger.doLog(Guid.NewGuid().ToString(), ge.ToString(gacVerification.VerificationFilter), Logger.ServerityLevel.ERROR);
                            }
                        }
                    }
                    //gacLists.Add(btsServer.ComputerName, gacList);
            
                }


            }
            catch (Exception ex)
            {
                blnOperationSuccessful = false;
                outException = ex;
            }
            finally
            {
                outException = null;                
            }
            this.tbProgress.Text += "Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine;
            Logger.doLog(Guid.NewGuid().ToString(), "Operation " + (blnOperationSuccessful ? "successful" : "failed"), Logger.ServerityLevel.INFO);
            if (!blnOperationSuccessful && outException != null)
            {
                this.tbProgress.Text += outException.Message + System.Environment.NewLine;
                this.tbProgress.Text += outException.StackTrace + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), outException.Message, Logger.ServerityLevel.ERROR);
                Logger.doLog(Guid.NewGuid().ToString(), outException.StackTrace, Logger.ServerityLevel.ERROR);
            }
            this.tbProgress.Text += "|" + System.Environment.NewLine;
            this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
            Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);
        }

        private void PerformAction_BindingInfo(Operation op, string sEnvID, string dbServerName, string mgntDbName)
        {
            Exception outException = null;
            BindingInfoCollection bindingInfoCollection = null;
            bool blnOperationSuccessful = false;
            try
            {
                LogHelper(
                "--------------------------------------------------------------------------------------------" + System.Environment.NewLine
                + "|" + System.Environment.NewLine,
                Logger.ServerityLevel.INFO
                );
                bindingInfoCollection = (BindingInfoCollection)op.Item;
                foreach (BindingInfo bindingInfo in bindingInfoCollection.BindingInfo)
                {
                    this.tbProgress.Text += "Found operation [" + op.ExecSequence + "] type : " + bindingInfo.GetType().Name + ".Import........";
                    Logger.doLog(Guid.NewGuid().ToString(), "Found operation [" + op.ExecSequence + "] type : " + bindingInfo.GetType().Name + ".Import........", Logger.ServerityLevel.INFO);
                    try
                    {
                        blnOperationSuccessful = Library.BtsBindingInfo.Import
                    (new Nullable<bool>(true),
                    dbServerName,
                    mgntDbName,
                    bindingInfo.ApplicationName,
                    bindingInfo.FileSource);

                    }
                    catch (Exception exception)
                    {
                        blnOperationSuccessful = false;
                        outException = exception;
                    }
                    this.tbProgress.Text += "Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine;
                    Logger.doLog(Guid.NewGuid().ToString(), "Operation " + (blnOperationSuccessful ? "successful" : "failed"), Logger.ServerityLevel.INFO);
                    if (!blnOperationSuccessful && outException != null)
                    {
                        this.tbProgress.Text += outException.Message + System.Environment.NewLine;
                        this.tbProgress.Text += outException.StackTrace + System.Environment.NewLine;
                        Logger.doLog(Guid.NewGuid().ToString(), outException.Message, Logger.ServerityLevel.ERROR);
                        Logger.doLog(Guid.NewGuid().ToString(), outException.StackTrace, Logger.ServerityLevel.ERROR);
                    }
                }
                this.tbProgress.Text += "|" + System.Environment.NewLine;
                this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);

            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                outException = null;
                bindingInfoCollection = null;
            }
        }

        private void PerformAction_OrphanOrchestrationManagement(Operation op, string sEnvID, string dbServerName, string mgntDbName)
        {
            Exception outException = null;
            OrphanOrchestrationManagement osm = null;
            Orchestration[] orchs = null;
            try
            {
                bool blnOperationSuccessful = false;
                this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);
                this.tbProgress.Text += "|" + System.Environment.NewLine;
                osm = (OrphanOrchestrationManagement)op.Item;
                orchs = osm.RemovalExclusions;
                this.tbProgress.Text += "Found operation [" + op.ExecSequence + "] type : " + osm.GetType().Name + ".Removal........";
                Logger.doLog(Guid.NewGuid().ToString(), "Found operation [" + op.ExecSequence + "] type : " + osm.GetType().Name + ".Removal........", Logger.ServerityLevel.INFO);
                try
                {
                    blnOperationSuccessful = Library.OrphanOrchestrationManagement.RemoveOrchestration(orchs, dbServerName, mgntDbName);
                }
                catch (Exception exception)
                {
                    blnOperationSuccessful = false;
                    outException = exception;
                }
                this.tbProgress.Text += "Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "Operation " + (blnOperationSuccessful ? "successful" : "failed"), Logger.ServerityLevel.INFO);
                if (!blnOperationSuccessful && outException != null)
                {
                    this.tbProgress.Text += outException.Message + System.Environment.NewLine;
                    this.tbProgress.Text += outException.StackTrace + System.Environment.NewLine;
                    Logger.doLog(Guid.NewGuid().ToString(), outException.Message, Logger.ServerityLevel.ERROR);
                    Logger.doLog(Guid.NewGuid().ToString(), outException.StackTrace, Logger.ServerityLevel.ERROR);
                }
                this.tbProgress.Text += "|" + System.Environment.NewLine;
                this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);

            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                outException = null;
                osm = null;
                orchs = null;
            }
        }

        private void PerformAction_OrchestrationStateManagement(Operation op, string sEnvID, string dbServerName, string mgntDbName)
        {
            Exception outException = null;
            OrchestrationStateManagement osm = null;
            Orchestration[] oAllOrchs = null;
            Orchestration[] orchs = null;
            try
            {
                bool blnOperationSuccessful = false;
                this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);
                this.tbProgress.Text += "|" + System.Environment.NewLine;
                osm = (OrchestrationStateManagement)op.Item;
                #region ---- Don't Need -----
                //    if (osm.Orchestrations != null)
                //    {
                //        Orchestration[] orchs = osm.Orchestrations;
                //        //foreach (Orchestration orch in orchs)
                //        //{
                //        //    this.tbProgress.Text += "Found operation [" + op.ExecSequence + "] type : " + orch.GetType().Name + "." + orch.Status + "........";
                //        //    try
                //        //    {
                //        //        blnOperationSuccessful = Library.OrchestrationStateManagement.UpdateOrchestration(orch, dbServerName, mgntDbName);
                //        //    }
                //        //    catch (Exception exception)
                //        //    {
                //        //        blnOperationSuccessful = false;
                //        //        outException = exception;
                //        //    }
                //        //    this.tbProgress.Text += "Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine;
                //        //    if (!blnOperationSuccessful && outException != null)
                //        //    {
                //        //        this.tbProgress.Text += outException.Message + System.Environment.NewLine;
                //        //        this.tbProgress.Text += outException.StackTrace + System.Environment.NewLine;
                //        //    }
                //        //}
                //        this.tbProgress.Text += "Found operation [" + op.ExecSequence + "] type ........";
                //        System.Diagnostics.Debug.WriteLine("Found operation [" + op.ExecSequence + "] type ........");
                //        try
                //            {
                //                blnOperationSuccessful = Library.OrchestrationStateManagement.UpdateOrchestrations(orchs, dbServerName, mgntDbName);
                //            }
                //            catch (Exception exception)
                //            {
                //                blnOperationSuccessful = false;
                //                outException = exception;
                //            }
                //            this.tbProgress.Text += "Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine;
                //            System.Diagnostics.Debug.WriteLine("Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine);
                //            if (!blnOperationSuccessful && outException != null)
                //            {
                //                this.tbProgress.Text += outException.Message + System.Environment.NewLine;
                //                this.tbProgress.Text += outException.StackTrace + System.Environment.NewLine;
                //            }
                //    }
                //    if (osm.UnenlistExclusions != null)
                //    {
                //        Orchestration[] orchs = osm.UnenlistExclusions;
                //        this.tbProgress.Text += "Found operation [" + op.ExecSequence + "] type : " + orchs[0].GetType().Name + ".UnenlistExclusions........";
                //        System.Diagnostics.Debug.WriteLine("Found operation [" + op.ExecSequence + "] type : " + orchs[0].GetType().Name + ".UnenlistExclusions........");
                //        try
                //        {
                //            blnOperationSuccessful = Library.OrchestrationStateManagement.UpdateOrchestrations(
                //              Library.OrchestrationStateManagement.GetToBeUnenlistedList(orchs, dbServerName, mgntDbName),
                //              dbServerName, mgntDbName);

                //        }
                //        catch (Exception exception)
                //        {
                //            blnOperationSuccessful = false;
                //            outException = exception;
                //        }
                //        this.tbProgress.Text += "Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine;
                //        System.Diagnostics.Debug.WriteLine("Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine);
                //        if (!blnOperationSuccessful && outException != null)
                //        {
                //            this.tbProgress.Text += outException.Message + System.Environment.NewLine;
                //            this.tbProgress.Text += outException.StackTrace + System.Environment.NewLine;
                //        }
                //    }
                //    this.tbProgress.Text += "|" + System.Environment.NewLine;
                //    this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
                //}
                #endregion
                if (osm.Orchestrations != null)
                {

                    orchs = osm.Orchestrations;
                    #region ---- Don't Need -----
                    //foreach (Orchestration orch in orchs)
                    //{
                    //    this.tbProgress.Text += "Found operation [" + op.ExecSequence + "] type : " + orch.GetType().Name + "." + orch.Status + "........";
                    //    try
                    //    {
                    //        blnOperationSuccessful = Library.OrchestrationStateManagement.UpdateOrchestration(orch, dbServerName, mgntDbName);
                    //    }
                    //    catch (Exception exception)
                    //    {
                    //        blnOperationSuccessful = false;
                    //        outException = exception;
                    //    }
                    //    this.tbProgress.Text += "Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine;
                    //    if (!blnOperationSuccessful && outException != null)
                    //    {
                    //        this.tbProgress.Text += outException.Message + System.Environment.NewLine;
                    //        this.tbProgress.Text += outException.StackTrace + System.Environment.NewLine;
                    //    }
                    //}
                    #endregion
                    this.tbProgress.Text += "Found operation [" + op.ExecSequence + "] Orchestration State Management ........";
                    Logger.doLog(Guid.NewGuid().ToString(), "Found operation [" + op.ExecSequence + "] Orchestration State Management........", Logger.ServerityLevel.INFO);
                    if (osm.UnenlistExclusions != null)
                    {
                        #region --- Don't Need ---
                        //System.Diagnostics.Debug.WriteLine("Starting to combine orchestrations..");
                        //Logger.doLog(Guid.NewGuid().ToString(), "Starting to combine orchestrations..", Logger.ServerityLevel.INFO);
                        //Orchestration[] orchs2 = osm.UnenlistExclusions;
                        //oAllOrchs = (Orchestration[])ConcatenateArrays(orchs, Library.OrchestrationStateManagement.GetToBeUnenlistedList(orchs2, dbServerName, mgntDbName));
                        //System.Diagnostics.Debug.WriteLine("Done combining orchestrations..");
                        //Logger.doLog(Guid.NewGuid().ToString(), "Done combining orchestrations..", Logger.ServerityLevel.INFO);

                        //Logger.doLog(
                        //    Guid.NewGuid().ToString(),
                        //    "=================================================================================="
                        //    + System.Environment.NewLine
                        //    + "Final Orchestration State List: "
                        //    + System.Environment.NewLine,
                        //    Logger.ServerityLevel.INFO);
                        //foreach (Orchestration orch in orchs2)
                        //{
                        //    Logger.doLog(
                        //        Guid.NewGuid().ToString(),
                        //        " ===> "
                        //        + "(" + orch.Status + ") ==> "
                        //        + orch.Fullname
                        //        + System.Environment.NewLine,
                        //        Logger.ServerityLevel.INFO);
                        //}
                        
                        #endregion
                        oAllOrchs = (Orchestration[])ConcatenateArrays(orchs);
                    }
                    else
                    {
                        oAllOrchs = (Orchestration[])ConcatenateArrays(orchs);
                    }
                    try
                    {
                        System.Diagnostics.Debug.WriteLine("Starting to update orchestrations status..");
                        Logger.doLog(Guid.NewGuid().ToString(), "Starting to update orchestrations status..", Logger.ServerityLevel.INFO);
                        blnOperationSuccessful = Library.OrchestrationStateManagement.UpdateOrchestrations(oAllOrchs, dbServerName, mgntDbName);
                        System.Diagnostics.Debug.WriteLine("Done updating orchestrations status..");
                        Logger.doLog(Guid.NewGuid().ToString(), "Done updating orchestrations status..", Logger.ServerityLevel.INFO);
                    }
                    catch (Exception exception)
                    {
                        blnOperationSuccessful = false;
                        outException = exception;
                    }
                    this.tbProgress.Text += "Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine;
                    System.Diagnostics.Debug.WriteLine("Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine);
                    Logger.doLog(Guid.NewGuid().ToString(), "Operation " + (blnOperationSuccessful ? "successful" : "failed"), Logger.ServerityLevel.INFO);
                    if (!blnOperationSuccessful && outException != null)
                    {
                        this.tbProgress.Text += outException.Message + System.Environment.NewLine;
                        this.tbProgress.Text += outException.StackTrace + System.Environment.NewLine;
                        Logger.doLog(Guid.NewGuid().ToString(), outException.Message, Logger.ServerityLevel.ERROR);
                        Logger.doLog(Guid.NewGuid().ToString(), outException.StackTrace, Logger.ServerityLevel.ERROR);
                    }
                }
                #region --- Don't Need ---
                //if (osm.UnenlistExclusions != null && osm.Orchestrations == null)
                //{
                //    Orchestration[] orchs = osm.UnenlistExclusions;
                //    this.tbProgress.Text += "Found operation [" + op.ExecSequence + "] type : " + orchs[0].GetType().Name + ".UnenlistExclusions........";
                //    System.Diagnostics.Debug.WriteLine("Found operation [" + op.ExecSequence + "] type : " + orchs[0].GetType().Name + ".UnenlistExclusions........");
                //    Logger.doLog(Guid.NewGuid().ToString(), "Found operation [" + op.ExecSequence + "] type : " + orchs[0].GetType().Name + ".UnenlistExclusions........", Logger.ServerityLevel.INFO);
                //    try
                //    {
                //        blnOperationSuccessful = Library.OrchestrationStateManagement.UpdateOrchestrations(
                //          Library.OrchestrationStateManagement.GetToBeUnenlistedList(orchs, dbServerName, mgntDbName),
                //          dbServerName, mgntDbName);

                //    }
                //    catch (Exception exception)
                //    {
                //        blnOperationSuccessful = false;
                //        outException = exception;
                //    }
                //    this.tbProgress.Text += "Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine;
                //    System.Diagnostics.Debug.WriteLine("Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine);
                //    Logger.doLog(Guid.NewGuid().ToString(), "Operation " + (blnOperationSuccessful ? "successful" : "failed"), Logger.ServerityLevel.INFO);
                //    if (!blnOperationSuccessful && outException != null)
                //    {
                //        this.tbProgress.Text += outException.Message + System.Environment.NewLine;
                //        this.tbProgress.Text += outException.StackTrace + System.Environment.NewLine;
                //        Logger.doLog(Guid.NewGuid().ToString(), outException.Message, Logger.ServerityLevel.ERROR);
                //        Logger.doLog(Guid.NewGuid().ToString(), outException.StackTrace, Logger.ServerityLevel.ERROR);
                //    }
                //}
                
                #endregion                this.tbProgress.Text += "|" + System.Environment.NewLine;
                this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);

            }
            catch (Exception ex)
            {
                
                throw ex;
            }
            finally
            {
                outException = null;
                osm = null;
                oAllOrchs = null;
                orchs = null;
            }
        }

        private void PerformAction_RuleSetDeployment(Operation op, string sEnvID, string dbServerName, string mgntDbName)
        {
            Exception outException = null;
            bool blnOperationSuccessful = false;
            this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
            this.tbProgress.Text += "|" + System.Environment.NewLine;
            Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);
            RuleSetDeployment oRSDeploy = (RuleSetDeployment)op.Item;
            if (oRSDeploy.BtsRuleSet != null)
            {
                foreach (BtsRuleSet opr in oRSDeploy.BtsRuleSet)
                {
                    this.tbProgress.Text += "Found operation [" + op.ExecSequence + "] type : " + oRSDeploy.GetType().Name + ".PublishedRule........";
                    Logger.doLog(Guid.NewGuid().ToString(), "Found operation [" + op.ExecSequence + "] type : " + oRSDeploy.GetType().Name + ".PublishedRule........", Logger.ServerityLevel.INFO);
                    try
                    {
                        blnOperationSuccessful = Library.RuleSetDeployment.Exec(opr.Action, opr.RuleName, new Nullable<int>(opr.MajorVersion), new Nullable<int>(opr.MinorVersion));

                    }
                    catch (Exception exception)
                    {
                        blnOperationSuccessful = false;
                        outException = exception;
                    }
                    this.tbProgress.Text += "Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine;
                    Logger.doLog(Guid.NewGuid().ToString(), "Operation " + (blnOperationSuccessful ? "successful" : "failed"), Logger.ServerityLevel.INFO);
                    if (!blnOperationSuccessful && outException != null)
                    {
                        this.tbProgress.Text += outException.Message + System.Environment.NewLine;
                        this.tbProgress.Text += outException.StackTrace + System.Environment.NewLine;
                        Logger.doLog(Guid.NewGuid().ToString(), outException.Message, Logger.ServerityLevel.ERROR);
                        Logger.doLog(Guid.NewGuid().ToString(), outException.StackTrace, Logger.ServerityLevel.ERROR);
                    }
                }
            }
            else if (oRSDeploy.RulesFromFile != null)
            {
                foreach (RulesFromFile orff in oRSDeploy.RulesFromFile)
                {
                    this.tbProgress.Text += "Found operation [" + op.ExecSequence + "] type : " + oRSDeploy.GetType().Name + ".RulesFromFile........";
                    this.tbProgress.Text += "\t--> Operation has not been implemented yet." + System.Environment.NewLine;
                    Logger.doLog(Guid.NewGuid().ToString(), "Found operation [" + op.ExecSequence + "] type : " + oRSDeploy.GetType().Name + ".RulesFromFile........", Logger.ServerityLevel.INFO);
                    Logger.doLog(Guid.NewGuid().ToString(), "\t--> Operation has not been implemented yet.", Logger.ServerityLevel.INFO);
                }
            }
            this.tbProgress.Text += "|" + System.Environment.NewLine;
            this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
            Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);
        }

        private void PerformAction_MSBTS_Orchestration(Operation op, string sEnvID, string dbServerName, string mgntDbName)
        {
            Exception outException = null;
            bool blnOperationSuccessful = false;
            List<ManagementBaseObject> outParam = new List<ManagementBaseObject>();
            List<ManagementBaseObject> OutputParams = new List<ManagementBaseObject>();

            MSBTS_Orchestration orch = (MSBTS_Orchestration)op.Item;
            if (orch.InvokeMethod.Equals(Library.OrchestrationMethods.Enlist.ToString()))
                //&& op.ExecSequence == iLoopCount)
            {
                this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
                this.tbProgress.Text += "|" + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);
                this.tbProgress.Text += "Found operation [" + op.ExecSequence + "] type : " + orch.GetType().Name + "." + orch.InvokeMethod + "........";
                Logger.doLog(Guid.NewGuid().ToString(), "Found operation [" + op.ExecSequence + "] type : " + orch.GetType().Name + "." + orch.InvokeMethod + "........", Logger.ServerityLevel.INFO);
                try
                {
                    blnOperationSuccessful = Library.MSBTS_Orchestration.Enlist(orch, ref outParam, ref outException);
                }
                catch (Exception exception)
                {
                    blnOperationSuccessful = false;
                    outException = exception;
                }
                this.tbProgress.Text += "Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "Operation " + (blnOperationSuccessful ? "successful" : "failed"), Logger.ServerityLevel.INFO);
                if (!blnOperationSuccessful && outException != null)
                {
                    this.tbProgress.Text += outException.Message + System.Environment.NewLine;
                    this.tbProgress.Text += outException.StackTrace + System.Environment.NewLine;
                    Logger.doLog(Guid.NewGuid().ToString(), outException.Message, Logger.ServerityLevel.ERROR);
                    Logger.doLog(Guid.NewGuid().ToString(), outException.StackTrace, Logger.ServerityLevel.ERROR);
                }
                this.tbProgress.Text += "|" + System.Environment.NewLine;
                this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);
            }
            if (orch.InvokeMethod.Equals(Library.OrchestrationMethods.Unenlist.ToString()))
                //&& op.ExecSequence == iLoopCount)
            {
                this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
                this.tbProgress.Text += "|" + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);
                this.tbProgress.Text += "Found operation [" + op.ExecSequence + "] type : " + orch.GetType().Name + "." + orch.InvokeMethod + "........";
                Logger.doLog(Guid.NewGuid().ToString(), "Found operation [" + op.ExecSequence + "] type : " + orch.GetType().Name + "." + orch.InvokeMethod + "........", Logger.ServerityLevel.INFO);
                try
                {
                    blnOperationSuccessful = Library.MSBTS_Orchestration.Unenlist(orch, ref outParam, ref outException);
                }
                catch (Exception exception)
                {
                    blnOperationSuccessful = false;
                    outException = exception;
                }
                this.tbProgress.Text += "Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "Operation " + (blnOperationSuccessful ? "successful" : "failed"), Logger.ServerityLevel.INFO);
                if (!blnOperationSuccessful && outException != null)
                {
                    this.tbProgress.Text += outException.Message + System.Environment.NewLine;
                    this.tbProgress.Text += outException.StackTrace + System.Environment.NewLine;
                    Logger.doLog(Guid.NewGuid().ToString(), outException.Message, Logger.ServerityLevel.ERROR);
                    Logger.doLog(Guid.NewGuid().ToString(), outException.StackTrace, Logger.ServerityLevel.ERROR);
                }
                this.tbProgress.Text += "|" + System.Environment.NewLine;
                this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);
            }
            if (orch.InvokeMethod.Equals(Library.OrchestrationMethods.Start.ToString()))
           //&& op.ExecSequence == iLoopCount)
            {
                this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
                this.tbProgress.Text += "|" + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);
                this.tbProgress.Text += "Found operation [" + op.ExecSequence + "] type : " + orch.GetType().Name + "." + orch.InvokeMethod + "........";
                Logger.doLog(Guid.NewGuid().ToString(), "Found operation [" + op.ExecSequence + "] type : " + orch.GetType().Name + "." + orch.InvokeMethod + "........", Logger.ServerityLevel.INFO);
                try
                {
                    blnOperationSuccessful = Library.MSBTS_Orchestration.Start(orch, ref outParam, ref outException);
                }
                catch (Exception exception)
                {
                    blnOperationSuccessful = false;
                    outException = exception;
                }
                this.tbProgress.Text += "Operation " + (blnOperationSuccessful ? "successful" : "failed") + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "Operation " + (blnOperationSuccessful ? "successful" : "failed"), Logger.ServerityLevel.INFO);
                if (!blnOperationSuccessful && outException != null)
                {
                    this.tbProgress.Text += outException.Message + System.Environment.NewLine;
                    this.tbProgress.Text += outException.StackTrace + System.Environment.NewLine;
                    Logger.doLog(Guid.NewGuid().ToString(), outException.Message, Logger.ServerityLevel.ERROR);
                    Logger.doLog(Guid.NewGuid().ToString(), outException.StackTrace, Logger.ServerityLevel.ERROR);
                }
                this.tbProgress.Text += "|" + System.Environment.NewLine;
                this.tbProgress.Text += "--------------------------------------------------------------------------------------------" + System.Environment.NewLine;
                Logger.doLog(Guid.NewGuid().ToString(), "--------------------------------------------------------------------------------------------", Logger.ServerityLevel.INFO);
            }
        }

        private void btnPerformAction_Click(object sender, EventArgs e)
        {
            string dbServerName = ".";
            string mgntDbName = "BizTalkMgmtDb";
            string sEnvID = "";
            this.tbProgress.Text = string.Empty;
            //Exception outException = null;
            //bool blnOperationSuccessful = false;
            //List<ManagementBaseObject> outParam = new List<ManagementBaseObject>();
            //List<ManagementBaseObject> OutputParams = new List<ManagementBaseObject>();
            //XmlDocument oDoc = new XmlDocument();

            //string sXmlFileName = this.tbFilename.Text.Trim();
            LogHelper(
                    System.Environment.NewLine 
                        + "Starting " 
                        + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") 
                        + System.Environment.NewLine,
                    Logger.ServerityLevel.INFO
                    );
            try
            {
                if (this.cbEnvInfo.Items != null && this.cbEnvInfo.Items.Count > 0 && envInfos!=null)
                {
                    foreach (Environment env in envInfos)
                    {
                        if (env.EnvID.Equals(this.cbEnvInfo.SelectedItem.ToString()))
                        {
                            dbServerName = env.BtsDbServerName;
                            mgntDbName = env.BtsManagmentDbName;
                            break;
                        }
                    }
                    sEnvID = this.cbEnvInfo.SelectedItem.ToString();
                }
                //oDoc.Load(sXmlFileName);

                //MSBTSOperations oOps = (MSBTSOperations)
                //        GML.Common.Utilities.Xml.Deserialize2Object(oDoc.InnerXml.Trim(), typeof(MSBTSOperations));

                //this.tbProgress.Text += "Finished loading XML." + System.Environment.NewLine;
                int iLoopCount = 0;
                while (iLoopCount < oOps.Operation.Length)
                {
                    foreach (Operation op in oOps.Operation)
                    {
                        if (op.Item.GetType().Equals(typeof(GML.BizTalk.OperationManager.MSBTSOperations.ReceiveLocations))
                                && op.ExecSequence == iLoopCount)
                        {
                            if (this.cb_lastBTSDeployment.SelectedItem.ToString().ToUpper().Equals("YES"))
                            {
                                this.PerformAction_ReceiveLocations(op, sEnvID, dbServerName, mgntDbName);
                            }
                            else
                            {
                                LogHelper(
                                    "Skipping operation [" + op.ExecSequence + "] for ReceiveLocations because this is not the last bizTalk deployment: ........" + System.Environment.NewLine,
                                    Logger.ServerityLevel.INFO
                                    );
                            }
                        }
                        if (op.Item.GetType().Equals(typeof(GML.BizTalk.OperationManager.MSBTSOperations.SendPorts))
                                && op.ExecSequence == iLoopCount)
                        {
                            if (this.cb_lastBTSDeployment.SelectedItem.ToString().ToUpper().Equals("YES"))
                            {
                                this.PerformAction_SendPorts(op, sEnvID, dbServerName, mgntDbName);
                            }
                            else
                            {
                                LogHelper(
                                    "Skipping operation [" + op.ExecSequence + "] for SendPorts because this is not the last bizTalk deployment: ........" + System.Environment.NewLine,
                                    Logger.ServerityLevel.INFO
                                    );
                            }
                        }
                        if (op.Item.GetType().Equals(typeof(GML.BizTalk.OperationManager.MSBTSOperations.HostInstances))
                                && op.ExecSequence == iLoopCount)
                        {
                            if (this.cb_lastBTSDeployment.SelectedItem.ToString().ToUpper().Equals("YES"))
                            {
                                this.PerformAction_HostInstances(op, sEnvID, dbServerName, mgntDbName);
                            }
                            else
                            {
                                LogHelper(
                                    "Skipping operation [" + op.ExecSequence + "] for host instance(s) because this is not the last bizTalk deployment: ........" + System.Environment.NewLine,
                                    Logger.ServerityLevel.INFO
                                    );
                            }
                        }
                        if (op.Item.GetType().Equals(typeof(GML.BizTalk.OperationManager.MSBTSOperations.ServiceInstances))
                                && op.ExecSequence == iLoopCount)
                        {
                            if (this.cb_lastBTSDeployment.SelectedItem.ToString().ToUpper().Equals("YES"))
                            {
                                this.PerformAction_ServiceInstances(op, sEnvID, dbServerName, mgntDbName);
                            }
                            else
                            {
                                LogHelper(
                                    "Skipping operation [" + op.ExecSequence + "] for ServiceInstances because this is not the last bizTalk deployment: ........" + System.Environment.NewLine,
                                    Logger.ServerityLevel.INFO
                                    );
                            }
                        }
                        if (op.Item.GetType().Name.Equals(Library.OperationsType.BindingInfo.ToString())
                                && op.ExecSequence == iLoopCount)
                        {
                            this.PerformAction_BindingInfo(op, sEnvID, dbServerName, mgntDbName);
                        }
                        if (op.Item.GetType().Equals(typeof(GML.BizTalk.OperationManager.Library.OrphanOrchestrationManagement))
                                && op.ExecSequence == iLoopCount)
                        {
                            this.PerformAction_OrphanOrchestrationManagement(op, sEnvID, dbServerName, mgntDbName);
                        }
                        if (op.Item.GetType().Equals(typeof(GML.BizTalk.OperationManager.MSBTSOperations.OrchestrationStateManagement))
                                && op.ExecSequence == iLoopCount)
                        {
                            if (this.cb_lastBTSDeployment.SelectedItem.ToString().ToUpper().Equals("YES"))
                            {
                                this.PerformAction_OrchestrationStateManagement(op, sEnvID, dbServerName, mgntDbName);
                            }
                            else
                            {
                                LogHelper(
                                    "Skipping operation [" + op.ExecSequence + "] for OrchestrationStateManagement because this is not the last bizTalk deployment: ........" + System.Environment.NewLine,
                                    Logger.ServerityLevel.INFO
                                    );
                            }
                        }
                        if (op.Item.GetType().Equals(typeof(GML.BizTalk.OperationManager.MSBTSOperations.RuleSetDeployment))
                                && op.ExecSequence == iLoopCount)
                        {
                            this.PerformAction_RuleSetDeployment(op, sEnvID, dbServerName, mgntDbName);
                        }
                        if (op.Item.GetType().Name.Equals(Library.OperationsType.MSBTS_HostInstance.ToString())
                                && op.ExecSequence == iLoopCount)
                        {
                            /*
                                GetState - Retrieves the state of the given instance of the BizTalk host. 
                                Install - Installs a service for the given instance of the BizTalk host. 
                                Start - Starts the given instance of the BizTalk host. 
                                Stop - Stops the given instance of the BizTalk host. 
                                Uninstall - Uninstalls a service for the given instance of the BizTalk host. 
                             * */
                        }
                        if (op.Item.GetType().Name.Equals(Library.OperationsType.MSBTS_Host.ToString())
                                && op.ExecSequence == iLoopCount)
                        {
                            /*
                            Start - Starts all BizTalk Host instances in the BizTalk group for the given BizTalk Host. 
                            Stop - Stops all BizTalk Host instances in the BizTalk group for the given BizTalk Host. 
                             * */

                        }
                        if (op.Item.GetType().Name.Equals(Library.OperationsType.MSBTS_Orchestration.ToString())
                                && op.ExecSequence == iLoopCount)
                        {
                            this.PerformAction_MSBTS_Orchestration(op, sEnvID, dbServerName, mgntDbName);

                        }
                        if (op.Item.GetType().Name.Equals(Library.OperationsType.GACVerification.ToString())
                                && op.ExecSequence == iLoopCount)
                        {
                            this.PerformAction_GACListCompare(op, sEnvID, dbServerName, mgntDbName);

                        }

                    }
                    iLoopCount++;
                }

            }
            catch (Exception ex)
            {
                    this.tbProgress.Text += ex.Message + System.Environment.NewLine;
                    this.tbProgress.Text += ex.StackTrace + System.Environment.NewLine;
                    Logger.doLog(Guid.NewGuid().ToString(), ex.Message, Logger.ServerityLevel.ERROR);
                    Logger.doLog(Guid.NewGuid().ToString(), ex.StackTrace, Logger.ServerityLevel.ERROR);
                }
            this.tbProgress.Text += System.Environment.NewLine + "DONE! " + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + System.Environment.NewLine;
            Logger.doLog(Guid.NewGuid().ToString(), "DONE! " + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"), Logger.ServerityLevel.INFO);
        }

        private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
        {

        }

        private static Array ConcatenateArrays(params Array[] arrays)
        {
            if (arrays == null)
            {
                throw new ArgumentNullException("arrays");
            }
            if (arrays.Length == 0)
            {
                throw new ArgumentException("No arrays specified");
            }

            Type type = arrays[0].GetType().GetElementType();
            int totalLength = arrays[0].Length;
            for (int i = 1; i < arrays.Length; i++)
            {
                if (arrays[i].GetType().GetElementType() != type)
                {
                    throw new ArgumentException
                    ("Arrays must all be of the same type");
                }
                totalLength += arrays[i].Length;
            }

            Array ret = Array.CreateInstance(type, totalLength);
            int index = 0;
            foreach (Array array in arrays)
            {
                Array.Copy(array, 0, ret, index, array.Length);
                index += array.Length;
            }
            return ret;
        }


    }
}